package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.query.ClientQueryObject;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("client")
public class ClientController {

    @Autowired
    private IClientService clientService;

    @RequestMapping("potentialCustomerView")
    @RequiresPermissions(value = {"client:potentialCustomerView", "潜在客户页面"}, logical = Logical.OR)
    public String potentialCustomerView() {
        return "client/potentialCustomer";
    }

    @RequestMapping("potentialCustomerList")
    @ResponseBody
    public PageResult list(ClientQueryObject qo) {
        return clientService.list(qo);
    }

    @RequestMapping("potentialCustomerSaveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"client:potentialCustomerSaveOrUpdate","潜在客户新增/更改"},logical = Logical.OR)
    public JsonResult saveOrUpdate(Client obj) {
        try {
            if (obj.getId() != null) {
                clientService.updateByPrimaryKey(obj);
            } else {
                //保存新的员工要设置默认的状态为在职
                clientService.insert(obj);
            }
            return JsonResult.success("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("保存失败");
        }
    }

    @RequestMapping("moveIntoClientPool")
    @ResponseBody
    @RequiresPermissions(value = {"client:moveIntoClientPool","潜在客户移入资源池"},logical = Logical.OR)
    public JsonResult moveIntoClientPool(Long id) {
        try {
            clientService.moveIntoClientPool(id);
            return JsonResult.success("移入成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("移入失败");
        }
    }

    @RequestMapping("correctClient")
    @ResponseBody
    @RequiresPermissions(value = {"client:correctClient","潜在客户转正"},logical = Logical.OR)
    public JsonResult correctClient(Long id) {
        try {
            clientService.correctClient(id);
            return JsonResult.success("转正成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark(e.getMessage());
        }
    }

    @RequestMapping("officialClientView")
    @RequiresPermissions(value = {"officialClient:view", "正式客户页面"}, logical = Logical.OR)
    public String officialClientView() {
        return "client/officialClient";
    }

    @RequestMapping("officialClientUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"client:officialClientUpdate","潜在客户新增/更改"},logical = Logical.OR)
    public JsonResult officialClientUpdate(Client obj) {
        try {
            if (obj.getId() != null) {
                clientService.updateByPrimaryKey(obj);
            }
            return JsonResult.success("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("保存失败");
        }
    }

    @RequestMapping("clientPoolView")
    @RequiresPermissions(value = {"client:clientPoolView", "客户池页面"}, logical = Logical.OR)
    public String clientPoolView() {
        return "client/clientPool";
    }

    @RequestMapping("absorb")
    @ResponseBody
    @RequiresPermissions(value = {"client:absorb","吸纳客户"},logical = Logical.OR)
    public JsonResult absorb(Long id) {
        try {
            clientService.absorb(id);
            return JsonResult.success("吸纳成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("吸纳失败");
        }
    }

    @RequestMapping("selectByPrimaryKey")
    @RequiresPermissions(value = {"client:selectByPrimaryKey", "查询单个客户"}, logical = Logical.OR)
    public List<Client> selectByPrimaryKey(Long id) {
        List<Client> list = new ArrayList<>();
        list.add(clientService.selectByPrimaryKey(id));
        return list;
    }

}